package com.rico.gallery;


import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.rico.gallery.service.GalleryService;
import com.rico.gallery.utils.FileUtils;
import com.rico.gallery.view.DeviceFragment;
import com.rico.gallery.view.HomeFragment;
import com.rico.gallery.view.QRCodeFragment;
import com.rico.gallery.view.ScanFragment;
import com.rico.gallery.view.SettingFragment;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.util.ArrayList;
import java.util.List;



public class MainActivity extends FragmentActivity  implements ViewPager.OnPageChangeListener {

    protected static final String TAG="MainActivity";
    private GalleryService galleryService;
    private RadioGroup rgs;
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private ViewPager viewPager;

    public static MainActivity instance=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        instance=this;
        // 去掉界面任务条
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        initView();

        //连续启动Service
        Intent bindService = new Intent(this, GalleryService.class);
        bindService(bindService,conn,BIND_AUTO_CREATE);
        ZXingLibrary.initDisplayOpinion(this);

        FileUtils.checkPermission(MainActivity.this);


    }

    private void initView(){
        rgs = (RadioGroup) findViewById(R.id.tabs_rg);
        fragments.add(new HomeFragment());
        fragments.add(new QRCodeFragment());
        fragments.add(new SettingFragment());
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setOnPageChangeListener(this);
        viewPager.setAdapter(new IFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.setCurrentItem(0);//设置当前显示标签页为第一页
        RadioButton radioButton=(RadioButton)rgs.getChildAt(0);
        radioButton.setTextColor(getResources().getColor(R.color.radio_color_checked));
        rgs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int childCount=group.getChildCount();
                for(int i=0;i<childCount;i++){
                    RadioButton radioButton=(RadioButton)group.getChildAt(i);
                    if(radioButton.getId()!=checkedId){
                        radioButton.setTextColor(getResources().getColor(R.color.radio_color));
                    }else{
                        radioButton.setTextColor(getResources().getColor(R.color.radio_color_checked));
                    }
                }
                int fragmentIndex=0;
                switch(checkedId){
                    case R.id.radio_home:
                        fragmentIndex=0;
                        break;
                    case R.id.radio_device:
                        fragmentIndex=1;
                        break;
                    case R.id.radio_setting:
                        fragmentIndex=2;
                        break;
                }
                viewPager.setCurrentItem(fragmentIndex,false);
            }});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageSelected(int arg0) {
        for(int i=0;i<rgs.getChildCount();i++){
            RadioButton radioButton=(RadioButton)rgs.getChildAt(i);
            if(i!=arg0){
                radioButton.setChecked(false);
                radioButton.setTextColor(getResources().getColor(R.color.radio_color));
            }else{
                radioButton.setChecked(true);
                radioButton.setTextColor(getResources().getColor(R.color.radio_color_checked));
            }
        }
    }


    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.v(TAG,"服务已启动");
            GalleryService.MyBinder binder = (GalleryService.MyBinder) service;
            galleryService = binder.getService();
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.v("hjz","onServiceDisconnected  B");
        }
    };


}
